<%@ tag language="java" pageEncoding="ISO-8859-1"%>
<%@ attribute name="pagesize" required="true"%>
<%@ tag import="java.util.Date" 
		import="java.text.DateFormat"
		import="java.util.List" 
		import="ro.k.web.beans.order.ManoperaBaseBean"%>
		
<jsp:useBean id="tagDBMan" class="ro.k.tags.DBManopera"	scope="session" />
<jsp:useBean id="tagCmdMan" class="ro.k.tags.ComandaManopera"	scope="session" />

<script type="text/javascript" src="../js/validateManopera.js"></script>
<%
	final String PAGE_PARAM = "pm";
	final String NAME_PARAM = "n";
	final String CODE_PARAM = "c";
	final String SORT_PARAM = "s";
	final String HIDE_PARAM = "hidd";
	final String ADDM_PARAM = "m";
	
	int currPage = ro.k.web.beans.KBeanUtils.pageNr(request.getParameter(PAGE_PARAM));

	tagDBMan.setCodeFilter(request.getParameter(CODE_PARAM));
	tagDBMan.setNameFilter(request.getParameter(NAME_PARAM));
	tagDBMan.setSort(request.getParameter(SORT_PARAM));
	
	tagCmdMan.addManopera(request.getParameter(ADDM_PARAM));
	
	boolean hiddBool = tagDBMan.setHidden(request.getParameter(HIDE_PARAM));
	
	List<ManoperaBaseBean> li = tagDBMan.returnManopera();
	int pgsize = Integer.parseInt(pagesize);

	int nrPages = li.size() / pgsize;
	if (li.size() % pgsize != 0 || nrPages == 0) {
		nrPages++;
	}
	String page = request.getRequestURI().substring(
			request.getRequestURI().lastIndexOf("/") + 1);
%>

<h4 style="margin: 10px;">Manopera disponibila</h4>
<%
	if (!hiddBool) {
%>
<!-- Form pentru filtrare rezultate -->
<form action="<%=page%>">
	<table>
		<thead>
			<tr>
				<th>Cod</th>
				<th>Nume</th>
				<th>&nbsp;</th>
				<th>&nbsp;</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td><input type="text" name=<%=CODE_PARAM%> style="width: 45px;" /></td>
				<td><input type="text" name=<%=NAME_PARAM%> style="width: 296px;" /></td>
				<td><button type="submit">Cauta</button></td>
				<td>&nbsp;</td>
			</tr>
		</tbody>
	</table>
</form>
<!-- Tabel de afisare rezultate -->
<table>
	<thead>
		<tr class="imp">
			<th width="45px">
				<a href="<%=page + "?"+SORT_PARAM+"=c"%>">Cod </a>
				<%=tagDBMan.isCode() ? (tagDBMan.isSortAsc() ? "v" : "^")	: ""%></th>
			
			<th width="300px">
			<a href="<%=page + "?"+SORT_PARAM+"=n"%>">Nume </a>
			<%=tagDBMan.isName() ? (tagDBMan.isSortAsc() ? "v" : "^")	: ""%></th>			
			
			<th width="60px">Valoare</th>
			<th width="43px" style="background-color: #FF9696">
				<a href="<%=page + "?"+HIDE_PARAM+"=" + ro.k.tags.DBManopera.HIDE_TRUE %>" title="Particularizat" 
				style="color: white; text-decoration: none;">__X__</a>
			</th>
		</tr>
	</thead>
	<tbody>
		<%
				int i = (currPage - 1) * pgsize;
				int rowc = (i % 2) == 0 ? 1 : 0;
				for (; i < currPage * pgsize; i++) {
					if (i >= li.size()) {
		%>
		<tr <%out.print(((i + rowc) % 2 == 0) ? "" : "class=\"imp\"");%>>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>			
			<td>&nbsp;</td>
		</tr>
		<%			} else {		%>		
		<tr <%out.print(((i + rowc) % 2 == 0) ? "" : "class=\"imp\"");%>>
			<td><%=li.get(i).getCod()%></td>
			<td><%=li.get(i).getNume()%></td>			
			<td align="right"><%=li.get(i).getValoare()%></td>
			<td><a href="<%=page + "?"+ADDM_PARAM+"=" + li.get(i).getCod()%>">adauga</a>
			</td>
		</tr>
		<%		}	}
		%>
	</tbody>
	<tfoot>
		<tr>
			<td colspan="4" align="center" width="100%">
				<table>
					<tr>
						<td width="33%">
							<%  if (currPage > 1) { 	  %> 
								<a href="<%=page + "?"+PAGE_PARAM+"=1"%>">&lt;&lt; Prima</a> 
								<a href="<%=page + "?"+PAGE_PARAM+"=" + (currPage - 1)%>">&lt; Inapoi</a> 
							<%  } %>
						</td>
						
						<td width="<%=(nrPages == 1) ? 40 : 32%>%" align="center">
						<% out.println("Pagina " + currPage + " din " + nrPages); %></td>
						
						<td width="35%">
							<%	if (currPage < nrPages) {  %> 
								<a href="<%=page + "?"+PAGE_PARAM+"=" + (currPage + 1)%>">Inainte &gt;</a>
								<a href="<%=page + "?"+PAGE_PARAM+"=" + nrPages%>">Ultima &gt;&gt;</a> 
							<%	} %>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</tfoot>
</table>
<%
	} else {
%>
<div align="right">
	<h3>
		<a href="<%=page + "?"+HIDE_PARAM+"="+ro.k.tags.DBManopera.HIDE_FALE%>" title="Normativ">Normativ</a>
	</h3>
</div>
<form action="<%=request.getContextPath()%>/addCustMan" method="post">
<table>
	<thead>
	<tr>
		<th>Denumire</th>
		<th>Valoare</th>
		<th>&nbsp;</th>
	</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<input type="text" style="width: 345px" name="denumire" 
				id="denumireCustom"	 onblur="validateDenumire()" autocomplete="off"/>
			</td>
			<td>
				<input type="text" style="width: 60px; text-align: right;" name="valoare" 
				id="valoareCustom" onblur="validateValue()" autocomplete="off"/>
			</td>
			<td>
				<input type="hidden" value="<%=page%>" name="pagina"/>
				<input type="submit" value="Adauga" onclick="return validateCustomForm();"/>
			</td>
		</tr>
	</tbody>	
</table>
</form>
<%
	}
%>